import { decode as atob, encode as btoa } from 'base-64';

// base64转十六进制Hex
export const base64ToHex = (str: string = '') => {
  for (var i = 0, bin = atob(str.replace(/[ \r\n]+$/, '')), hex = []; i < bin.length; ++i) {
    let tmp = bin.charCodeAt(i).toString(16);
    if (tmp.length === 1) tmp = '0' + tmp;
    hex[hex.length] = tmp;
  }
  return hex.join('');
};

// 导出（CommonJS/ESM 兼容简单写法）
if (typeof module !== 'undefined' && module.exports) {
  module.exports = base64ToHex;
}

export const hex2dec = (str: string) => {
  return parseInt(str, 16).toString().padStart(2, '0');
};

export function parseCustomString(originInput: string): {
  type: string;
  booleanValue: boolean;
} {
  const input = base64ToHex(originInput);
  
  const numberStr = input.substring(0, 2);

  const booleanStr = input.substring(2, 4);

  const typeMap = {
    '01': 'app_manage',
    '02': 'hand_manage',
    '03': 'time_manage',
    '04': 'circle_manage',
  };

  const type = typeMap[numberStr];

  const booleanValue = booleanStr === '01';

  return {
    type,
    booleanValue,
  };
}
